package com.leetcode;

import java.util.Objects;

/**
 * 面试题 01.05. 一次编辑
 * 分情况讨论
 *
 * 2022-05-13 每日一题
 *
 * @author fy
 * @date 2022/5/13 17:33
 */
public class Solution01_05 {

    public boolean oneEditAway(String first, String second) {
        if (Objects.equals(first, second)) {
            return true;
        }
        int n1 = first.length();
        int n2 = second.length();
        if (Math.abs(n1 - n2) > 1) {
            return false;
        }
        if (n1 == n2) {
            int diff = 0;
            for (int i = 0; i < n1; i++) {
                if (first.charAt(i) != second.charAt(i)) {
                    diff++;
                    if (diff > 1) {
                        return false;
                    }
                }
            }
            return true;
        }

        int p = 0;
        int q = 0;
        int diff = 0;
        while (p < n1 && q < n2) {
            if (first.charAt(p) != second.charAt(q)) {
                if (n1 > n2) {
                    p++;
                } else {
                    q++;
                }
                diff++;
                if (diff > 1) {
                    return false;
                }
            } else {
                p++;
                q++;
            }
        }
        return true;
    }


    public static void main(String[] args) {
        String first = "horse";
        String second = "hors";
        boolean res = new Solution01_05().oneEditAway(first, second);
        System.out.println(res);
    }

}
